package com.qinsilk.axon.inventory

import com.qinsilk.axon.inventory.command.*
import com.qinsilk.axon.inventory.query.InventoryRepository
import org.axonframework.commandhandling.gateway.CommandGateway
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.servlet.mvc.support.RedirectAttributes

@Controller
@RequestMapping("/")
class WebController(
        private val inventoryRepository: InventoryRepository,
        private val commandGateway: CommandGateway
) {

    @GetMapping("/")
    fun index(model: Model): String {
        val inventories = inventoryRepository.findAll()
        model.addAttribute("inventories", inventories)
        return "index"
    }

    @GetMapping("/details/{inventoryId}")
    fun details(@PathVariable inventoryId: String, model: Model): String {
        val inventory = inventoryRepository.findById(inventoryId).orElse(null)
        model.addAttribute("inventory", inventory)
        return "details"
    }

    @PostMapping("/details/{inventoryId}")
    fun handleStockOperation(
            @PathVariable inventoryId: String,
            @RequestParam(required = false) quantity: Int?,
            @RequestParam(required = false) reason: String?,
            @RequestParam("_method") method: String,
            redirectAttributes: RedirectAttributes
    ): String {
        try {
            when (method) {
                "stock-out" -> {
                    if (quantity != null && reason != null) {
                        commandGateway.sendAndWait<String>(
                                StockOutCommand(inventoryId, quantity, reason)
                        )
                        redirectAttributes.addFlashAttribute("successMessage", "出库操作成功")
                    } else {
                        redirectAttributes.addFlashAttribute("errorMessage", "出库操作参数不完整")
                    }
                }
                "stock-in" -> {
                    if (quantity != null && reason != null) {
                        commandGateway.sendAndWait<String>(
                                StockInCommand(inventoryId, quantity, reason)
                        )
                        redirectAttributes.addFlashAttribute("successMessage", "入库操作成功")
                    } else {
                        redirectAttributes.addFlashAttribute("errorMessage", "入库操作参数不完整")
                    }
                }
                else -> {
                    redirectAttributes.addFlashAttribute("errorMessage", "不支持的操作类型")
                }
            }
        } catch (e: Exception) {
            redirectAttributes.addFlashAttribute("errorMessage", "操作失败: ${e.message}")
        }
        return "redirect:/details/${inventoryId}"
    }
}
